


package com.gc.server.controller;

import com.yf.request.SearchParam;

import java.util.Date;
import java.util.List;

import com.yf.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import com.gc.server.vo.CostApplyVo;
import com.gc.server.service.CostApplyService;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

import com.gc.server.enums.CostApplyTypeEnum;
import com.gc.server.enums.CostApplyStatusEnum;

/**
 * 费用申请Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Oct 21 17:34:54 CST 2020
 */
@RestController
@RequestMapping("/cost/apply")
public class CostApplyController {
    @Autowired
    private CostApplyService costApplyService;

    /**
     * 列表费用申请
     */
    @SysLog("查看费用申请")
    @RequestMapping("/list")
    @RequiresPermissions("cost:apply:list")
    public R list(@RequestBody SearchParam<CostApplyVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<CostApplyVo> list = costApplyService.findVo(params.getEntity());
        return R.ok(new PageInfo(list));
    }

    /**
     * 下载订单
     */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("cost:apply:list")
    public void down(@RequestBody SearchParam<CostApplyVo> params, HttpServletResponse response) {
        List<CostApplyVo> list = costApplyService.findVo(params.getEntity());
        costApplyService.downLoad(response, list);
    }


    /**
     * 查看费用申请
     */
    @SysLog("查看费用申请")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("cost:apply:info")
    public R info(@PathVariable("id") Long id) {
        CostApplyVo vo = costApplyService.getVo(id);

        return R.ok(vo);
    }

    /**
     * 保存费用申请
     */
    @SysLog("保存费用申请")
    @RequestMapping("/save")
    @RequiresPermissions("cost:apply:save")
    public R save(@RequestBody CostApplyVo vo) {
        costApplyService.saveVo(vo);
        vo.getEntity().setAdminName(vo.getEntity().getAdminName()+ " " +ShiroUtils.getUserEntity().getUsername());
        return R.ok(vo);
    }

    /**
     * 修改费用申请
     */
    @SysLog("修改费用申请")
    @RequestMapping("/update")
    @RequiresPermissions("cost:apply:update")
    public R update(@RequestBody CostApplyVo vo) {
        if(vo.getCostApply().getStatus().equals(CostApplyStatusEnum.PAY.getIndex())){
            vo.getCostApply().setPayTime(new Date());
        }
        vo.getEntity().setAdminName(ShiroUtils.getUserEntity().getUsername());
        costApplyService.updateVo(vo);
        return R.ok(vo);
    }

    /**
     * 删除费用申请
     */
    @SysLog("删除费用申请")
    @RequestMapping("/delete")
    @RequiresPermissions("cost:apply:delete")
    public R delete(Long[] ids) {
        costApplyService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有费用申请
     */
    @SysLog("所有费用申请")
    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order != null ? order : "id desc");
        List<CostApplyVo> list = costApplyService.findVo(null);
        return R.ok(list);
    }

    /**
     * 所有enums
     */
    @SysLog("所有enums")
    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String, Object> options = new HashMap();
        options.put("typeOptions", CostApplyTypeEnum.names());
        options.put("statusOptions", CostApplyStatusEnum.names());
        return R.ok(options);
    }


    /***********自定义开始***********/


    @GetMapping("/dmz/img")
    public void downImg(HttpServletResponse response,Long id){
        costApplyService.draw(response,id);
    }


}
